package engine.logic
{
	import engine.GameData;
	import engine.control.MenuList;
	import engine.data.ModelLocator;
	import engine.event.SceneEvent;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.newUi.EliteFBListLogic;
	import engine.newUi.EliteFBUI;
	import engine.newUi.FBTaskLogic;
	import engine.ui.AlertUI;
	
	import flash.events.IEventDispatcher;
	import flash.geom.Point;
	
	import newx.BaseObject;
	import newx.com.config.StaticIni;

	public class SceneTransfer extends BaseObject
	{
		private static var _self			:SceneTransfer;
		
		/**数据模型*/
		private 	var model				:ModelLocator;
		
		public var arrivedPoint			:Point;
		
		/**地图人物移动模型*/
//		private var mc						:MovieClip;
		
		/**是否传送*/
		public var isTransfer				:Boolean;
		
		
		public function SceneTransfer(target:IEventDispatcher=null) 
		{
			super(target);
			init();
		}
		
		public static function getInstance():SceneTransfer
		{
			if ( _self == null )
			{
				_self = new SceneTransfer();
			}
			return _self;
		}
		
		private function init():void
		{
			model	=	ModelLocator.getInstance();	
			model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SCENE_TRANSFER_CONSUME), 
				onConfirmTransfer);
			model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_TRANSFER_TIME),
				onMovePosition);
			
//			mc = ResManager.newResClass("custSkin_25_1") as MovieClip ;
		}
		
		/**
		 * 点击导航或传送
		 */
		public function tranferTo(sceneID:String):void
		{
			if( "" == sceneID )
			{
				return;
			}
			
			var sceneObj : Object = StaticIni.getIniObj( sceneID );
			if( null != sceneObj )
			{
				var sceneArray : Array = sceneObj[ "r" ].split(",");
				
				if( 4 == sceneArray[1] )
				{//类型，1传场景2开界面3回城堡4发消息
					SystemSender.getInstance().CustomMsg( sceneArray[2] );
					return;
				}
				if(7 == sceneArray[1])
				{
					//精英副本定位
					EliteFBListLogic.getInstance().intoFBSceneID(sceneArray[6],FBTaskLogic.PK_FB);
					EliteFBUI.getInstance().showOrHide();
					return;
				}

			}
			
			
			var sceneAry:Array = sceneID.split(",");
			var tmpSceneID:String = "";
			if (sceneAry.length < 1)
			{
				return;
			}
			else if (sceneAry.length > 1)
			{
				tmpSceneID = sceneAry[1];
			}
			else
			{
				tmpSceneID = sceneAry[0];
			}
			
			var curScene:String = String(GameData.SceneModel);
			
			// 当前已在目标场景，无需切换
			if (tmpSceneID == curScene)
			{
				var sceneName:String 	= StaticIni.getSwfLangStr("scenename_" + tmpSceneID);
				var message:String		= StaticIni.getSwfLangStrVar("UIStr_1000326", [sceneName]);
				var title:String		= StaticIni.getSwfLangStr("UIStr_1000324");
				AlertUI.showAlert(title, message, AlertUI.YES);
			}
			else
			{
				if(sceneID.indexOf(",") == -1)
				{
					var obj:Object=StaticIni.getIniObj("Scene_NeedLevel");
					if(obj)
					{
						var needLsvel	: Object 	= obj[sceneID];
						var myLevel		: int		= model.scene.Role.GetPropertiesEx("Level");
						if(myLevel < needLsvel)
						{//不能传送
							AlertUI.showAlert(StaticIni.getSwfLangStr("MessageUI_Notice"),StaticIni.getSwfLangStr("UIStr_1001106"));
							return;
						}
					}
				}
				
				// 向服务端发送传送请求消息
				SystemSender.getInstance().CustomMsg(
					CustomToServerFlag.CLIENT_CUSTOMMSG_SCENE_TRANSFER_REQUEST, sceneID);					
			}
		}
		
		/**
		 * 传送 
		 * @param sceneID
		 * 
		 */
		
		public function StartTransfer(sceneID:String,_placeName:String = null):void
		{
			var menuItemList	:Array 	= 	new Array();
			
			var clickData:Object 	= 	new Object();
			
			menuItemList.push(StaticIni.getSwfLangStr("UIStr_1000324"));
			if(_placeName != null)
			{
				menuItemList.push(StaticIni.getSwfLangStr("Common_Detail"));
				
				clickData.placeName 	=	_placeName;
			}
			
			menuItemList.push(StaticIni.getSwfLangStr("UIStr_1000010"));
			
			clickData.sceneID		=	sceneID;
			
			MenuList.ShowMenu(menuItemList, onLinkMenuClick, clickData);
			
		}
		
		/**
		 * 点击导航或传送
		 * @param i
		 * 
		 */		
		private function onLinkMenuClick(i:uint,data:Object=null):void
		{
			if (data == null)
			{
				return;	
			}
			
			// 传送
			if (i == 0)
			{
				var sceneID:String = data.sceneID;
				var sceneAry:Array = sceneID.split(",");
				var tmpSceneID:String = "";
				if (sceneAry.length < 1)
				{
					return;
				}
				else if (sceneAry.length > 1)
				{
					tmpSceneID = sceneAry[1];
				}
				else
				{
					tmpSceneID = sceneAry[0];
				}
				
				var curScene:String = String(GameData.SceneModel);
				
				// 当前已在目标场景，无需切换
				if (tmpSceneID == curScene)
				{
					var sceneName:String 	= StaticIni.getSwfLangStr("scenename_" + tmpSceneID);
					var message:String		= StaticIni.getSwfLangStrVar("UIStr_1000326", [sceneName]);
					var title:String		= StaticIni.getSwfLangStr("UIStr_1000324");
					AlertUI.showAlert(title, message, AlertUI.YES);
				}
				else
				{
					if(sceneID.indexOf(",") == -1)
					{
						var needLsvel	: Object 	= StaticIni.getIniObj("Scene_NeedLevel")[sceneID];
						var myLevel		: int		= model.scene.Role.GetPropertiesEx("Level");
						if(myLevel < needLsvel)
						{//不能传送
							AlertUI.showAlert(StaticIni.getSwfLangStr("MessageUI_Notice"),StaticIni.getSwfLangStr("UIStr_1001106"));
							return;
						}
					}
					tranferTo(sceneID);
					// 向服务端发送传送请求消息
//					SystemSender.getInstance().CustomMsg(
//						CustomToServerFlag.CLIENT_CUSTOMMSG_SCENE_TRANSFER_REQUEST, sceneID);
				}
				
			}
//			//查看详情
//			else if(i == 1 && Object(data).hasOwnProperty("placeName"))
//			{
//				var place:String 		= data.placeName;
//				
//				WorldMapNewUI.getInstance()._curtMapData = StaticIni.getIniObj(data.sceneID);
//				
//				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_OPENMAP_LEVELTWO,2);
//				
//				WorldMapNewUI.getInstance().loadMap("map_place" + place);
//				
//				WorldMapNewUI.getInstance().searchShow(false);
//			}
			
			MenuList.hide();
		}
		
		/**
		 * 确认传送消耗
		 * @param e
		 * 
		 */		
		private function onConfirmTransfer(e:CustomMsgEvent):void
		{
			var sceneID:String = e.pList[0];
			var sceneAry:Array = sceneID.split(",");
			var tmpSceneID:String = "";
			if (sceneAry.length < 1)
			{
				return;
			}
			else if (sceneAry.length > 1)
			{
				tmpSceneID = sceneAry[1];
			}
			else
			{
				tmpSceneID = sceneAry[0];
			}
			
			
			var costGoldCoin:int = e.pList[1];
			var type		: int = e.pList[2];
			var param		: int = e.pList[3];
			
			var sceneName:String = StaticIni.getSwfLangStr("scenename_" + tmpSceneID);
			var title:String	= StaticIni.getSwfLangStr("UIStr_1000324");
			var message:String	= StaticIni.getSwfLangStrVar("UIStr_1000327", 
				[sceneName, costGoldCoin]);
			
//			AlertUI.showAlert(title, message, AlertUI.OK|AlertUI.CANCEL, 
//				beginTransfer, [sceneID, costGoldCoin, type, param], false);
			beginTransfer( AlertUI.OK, "", [sceneID, costGoldCoin, type, param] );
		}
		
		/**
		 * 开始传送
		 * 
		 */
		private function beginTransfer(
			type:int, 
			str:String, 
			clickData:*):void
		{
			if (type == AlertUI.CANCEL)
			{
				return;
			}
			
			if (clickData == null)
			{
				return;
			}
			
			// 向服务端发送开始传送消息
			SystemSender.getInstance().CustomMsg(
				CustomToServerFlag.CLIENT_CUSTOMMSG_SCENE_TRANSFER_BEGIN, 
				clickData[0], clickData[1], clickData[2], clickData[3]);
			
			model.dispatchEvent(new SceneEvent(SceneEvent.SceneTransfer, null));
			
			MenuList.hide();
			
		}
		
		private function onMovePosition(e:CustomMsgEvent):void
		{
			e.pList
//			trace(e.pList)
//			var _time:int = e.pList[0];
//			var sceneId:String = e.pList[1];
//			if(_time != 0 )
//			{
//				if(WorldMapNewUI.getInstance().stage == null )
//				{
//					WorldMapNewUI.getInstance().show();
//				}
//				
//				setTimeout(function():void
//				{
//					var point:Point = new Point();
//					point.x = int(StaticIni.getIniVar(sceneId,"pointX"));
//					point.y = int(StaticIni.getIniVar(sceneId,"pointY"));
//					arrivedPoint = point;
//					
//					var sprite:Sprite = new Sprite();
//					WorldMapNewUI.getInstance().addChild(sprite);
//					
//					var bit:BitmapData = new BitmapData(6,6,true,0x00000000);
//					bit.draw(mc);
//					
//					var startX:int = int(StaticIni.getIniVar(model.scene.Role.GetPropertiesEx("SceneArea").split(",")[0],"pointX"));
//					var startY:int = int(StaticIni.getIniVar(model.scene.Role.GetPropertiesEx("SceneArea").split(",")[0],"pointY"));
//					
//					WorldMapNewUI.getInstance().logoWalk.x = startX;
//					WorldMapNewUI.getInstance().logoWalk.y = startY;
//					//起点坐标
//					var startPoint:Point = new Point(startX,startY);
//					
//					TweenLite.to(WorldMapNewUI.getInstance().logoWalk,_time,
//						{ 	x:arrivedPoint.x,
//							y:arrivedPoint.y,
//							ease:Linear.easeNone,
//							onStart:function():void
//							{
//								isTransfer = true;
//								WorldMapNewUI.getInstance().moveTips.visible = false;
//								WorldMapNewUI.getInstance().tipsLabel.visible = false;
//								
//								WorldMapNewUI.getInstance().logoWalk.visible = true;
//								WorldMapNewUI.getInstance().logo.visible = false;
//								if( startPoint.x >= arrivedPoint.x)
//								{
//									if(WorldMapNewUI.getInstance().direct == 0)
//									{
//										WorldMapNewUI.getInstance().logoWalk.scaleX = -1;
//										WorldMapNewUI.getInstance().direct = 1;
//									}
//								}
//								else if(WorldMapNewUI.getInstance().direct == 1)
//								{
//									WorldMapNewUI.getInstance().logoWalk.scaleX = 1;
//									WorldMapNewUI.getInstance().direct = 0;
//								}
//							},
//							onUpdate:function():void
//							{
//								var tempPoint:Point = new Point(WorldMapNewUI.getInstance().logoWalk.x,WorldMapNewUI.getInstance().logoWalk.y);
//								
//								var tipsPoint:Point = tempPoint.clone();
//								
//								tipsPoint.offset(10,-50);
//								
//								WorldMapNewUI.getInstance().moveTips.x = tipsPoint.x;
//								WorldMapNewUI.getInstance().moveTips.y = tipsPoint.y;
//								
//								WorldMapNewUI.getInstance().tipsLabel.x = tipsPoint.x;
//								WorldMapNewUI.getInstance().tipsLabel.y = tipsPoint.y;
//								
//								if( Point.distance(startPoint,tempPoint) > 10)
//								{
//									var _shape:Bitmap = new Bitmap(bit);
//									sprite.addChild(_shape);
//									_shape.x = WorldMapNewUI.getInstance().logoWalk.x;
//									_shape.y = WorldMapNewUI.getInstance().logoWalk.y;
//									
//									startPoint = tempPoint; 
//								}
//								
//							},
//							onComplete:function():void
//							{
//								isTransfer = false;
//								
//								WorldMapNewUI.getInstance().moveTips.visible = true;
//								WorldMapNewUI.getInstance().tipsLabel.visible = true;
//								
//								WorldMapNewUI.getInstance().logo.x = WorldMapNewUI.getInstance().logoWalk.x;
//								WorldMapNewUI.getInstance().logo.y = WorldMapNewUI.getInstance().logoWalk.y;
//								
//								WorldMapNewUI.getInstance().logoWalk.visible = false;
//								WorldMapNewUI.getInstance().logo.visible = true;
//								
//								arrivedPoint = null;
//								bit.dispose();
//								
//								while(sprite.numChildren > 0){
//									sprite.removeChildAt(0);
//								}
//								
//								if(sprite.parent) sprite.parent.removeChild(sprite);
//							}
//						});
//				},100);
//			}
		}
		
		/**
		 * 直接传送 
		 * @param sceneID
		 * 
		 */		
		public function directTransfer(sceneID:String):void
		{
			var clickData:Object 	= 	new Object();
			clickData.sceneID		=	sceneID;
			onLinkMenuClick(0,clickData);
		}
	}
}